#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Student{
public:

    Student(int code,char *name,int age)
        :_code(code),
        _name(new char[strlen(name)+1]()),
        _age(age)
    {
        if(name){
            strcpy(_name,name);
        }else {
            cout<<"name = 0"<<endl;
        }

    }

    Student(){}

    Student(const Student &rs){
        _code = rs._code;
        _name = new char[strlen(rs._name)+1]();
        if(rs._name){
            strcpy(_name,rs._name);
            _age = rs._age;
        }else{
            cout<<"rs._name = 0"<<endl;
        }
    }

    Student &operator=(const Student &rs){
        if(this != &rs){
            delete []_name;
            _code = rs._code;
            _name = new char[strlen(rs._name)+1]();
            if(rs._name){
                strcpy(_name,rs._name);
            }else{
                cout<<"rs._name = 0"<<endl;
            }
            _age = rs._age;
        }
        return *this;
    }

    ~Student(){
        delete []_name;//delete null是安全的
    }

    void print(){
        cout<<"name:"<<_name<<",code:"<<_code<<",age"<<_age<<endl;
    }

private:
    int _code;
    char *_name;
    int _age;
};

void test(){
    Student s1(1,"zs",18);
    Student s2(2,"ls",17); 
    Student s3;
    s3 = s1;
    Student s4 = s2;
    s1.print();
    s2.print();
    s3.print();
    s4.print();
}

int main()
{
    test ();
    return 0;
}

